package com.css.fxfzysx.modules.vaolEngineering.service.impl;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.css.fxfzysx.base.response.RestResponse;
import com.css.fxfzysx.constants.FxfzConstants;
import com.css.fxfzysx.constants.YNEnum;
import com.css.fxfzysx.modules.cztDataMange.result.ParsingResult;
import com.css.fxfzysx.modules.lifeline.annotation.ValidationProcessor;
import com.css.fxfzysx.modules.lifeline.entity.newEntity.SmxDevicesEntity;
import com.css.fxfzysx.modules.lifeline.entity.newEntity.SmxMainTableEntity;
import com.css.fxfzysx.modules.lifeline.entity.newEntity.SmxVersionEntity;
import com.css.fxfzysx.modules.lifeline.repository.LifelineEngineeringRepository;
import com.css.fxfzysx.modules.lifeline.utils.ShpUtils;
import com.css.fxfzysx.modules.lifeline.vo.basic.DestructiveStateVo;
import com.css.fxfzysx.modules.vaolEngineering.dto.BuildingHeightDto;
import com.css.fxfzysx.modules.vaolEngineering.dto.CaCeVulnerabilityDto;
import com.css.fxfzysx.modules.vaolEngineering.entity.newEntity.CztSmxDevicesValueEntity;
import com.css.fxfzysx.modules.vaolEngineering.param.update.DevicesParam;
import com.css.fxfzysx.modules.vaolEngineering.repository.CeVulnerabilityRepository;
import com.css.fxfzysx.modules.vaolEngineering.service.CeVulnerabilityService;
import com.css.fxfzysx.modules.vaolEngineering.utils.CheckUtil;
import com.css.fxfzysx.modules.vaolEngineering.utils.MainTableUtils;
import com.css.fxfzysx.modules.vaolEngineering.vo.CommunicationCountVo;
import com.css.fxfzysx.modules.vaolEngineering.vo.importVo.CsForceImportVo;
import com.css.fxfzysx.modules.ysxManagement.entity.CnRatioEntity;
import com.css.fxfzysx.modules.ysxManagement.entity.DynamicFieldsEntity;
import com.css.fxfzysx.modules.ysxManagement.entity.SrBaselineEntity;
import com.css.fxfzysx.modules.ysxManagement.repository.CecMainRepository;
import com.css.fxfzysx.modules.ysxManagement.repository.ZqtCiRepository;
import com.css.fxfzysx.util.PlatformDateUtils;
import com.css.fxfzysx.util.PlatformObjectUtils;
import com.css.fxfzysx.util.ShpAnalyticUtil;
import com.css.fxfzysx.util.UUIDGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class CeVulnerabilityServiceImpl implements CeVulnerabilityService {

    @Autowired
    private CeVulnerabilityRepository ceVulnerabilityRepository;

    @Autowired
    private CecMainRepository cecMainRepository;

    @Autowired
    private ZqtCiRepository zqtCiRepository;

    @Autowired
    private ShpUtils shpUtils;

    @Autowired
    private MainTableUtils mainTableUtils;

    @Autowired
    private LifelineEngineeringRepository lifelineEngineeringRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResponse caCeVulnerability(String versionId, String tableId) {
        try {
            List<SmxDevicesEntity> list = new ArrayList<>();
            Integer[] intensitys = {6, 7, 8, 9, 10};
            if (PlatformObjectUtils.isNotEmpty(tableId)) {
                SmxDevicesEntity entity = ceVulnerabilityRepository.getByTableId(tableId);
                list.add(entity);
            } else {
                list = ceVulnerabilityRepository.getByVersionId(versionId);
            }
            //k3
            List<DynamicFieldsEntity> k3Title = zqtCiRepository.getDynamicFields("通讯设备重要度系数");
            List<CnRatioEntity> allCnRatio = cecMainRepository.getAllCnRatio();
            //amx
            List<DynamicFieldsEntity> maxTitle = zqtCiRepository.getDynamicFields("地震影响系数基准值");
            List<SrBaselineEntity> allSrBaseline = cecMainRepository.getAllSrBaseline();

            for (SmxDevicesEntity it : list) {
                StringBuilder k3Ids = new StringBuilder();
                StringBuilder maxIds = new StringBuilder();
                for (Integer intensity : intensitys) {
                    CztSmxDevicesValueEntity valueEntity = new CztSmxDevicesValueEntity();
                    valueEntity.setId(UUIDGenerator.getUUID());
                    valueEntity.setDevicesId(it.getId());
                    valueEntity.setIntensity(intensity);
                    List<CaCeVulnerabilityDto> caCeVulnerabilityDtos;
                    List<BuildingHeightDto> buildingHeightDtos;
                    //处理k3数据
                    Map<String, DynamicFieldsEntity> map = k3Title.stream().collect(
                            Collectors.toMap(DynamicFieldsEntity::getCbrsId,
                                    w -> w));
                    caCeVulnerabilityDtos = allCnRatio.stream().map(h -> {
                        return toCaCeVulnerability(map.get(h.getProjectId()), h);
                    }).collect(Collectors.toList());

                    for (CaCeVulnerabilityDto dto : caCeVulnerabilityDtos) {
                        if (mainTableUtils.getValueByKey("smxtxsblb", it.getDeviceType()).equals(dto.getType()) && mainTableUtils.getValueByKey("smxtxsbszqy", it.getRegion()).equals(dto.getRegion())) {
                            String value = dto.getValue();
                            k3Ids.append(dto.getId());
                            if (value.contains("~")) {
                                BigDecimal bigDecimal = new BigDecimal(value.substring(0, value.indexOf("~"))).add(BigDecimal.valueOf(0.05));
                                valueEntity.setK3(bigDecimal);
                            } else {
                                valueEntity.setK3(new BigDecimal(value));
                            }
                        }
                    }
                    if (PlatformObjectUtils.isEmpty(valueEntity.getK3())) {
                        return RestResponse.fail("失败!请在 【通讯设备系数维护】 模块中维护 {通讯设备重要度系数} 信息");
                    }

                    //处理ao和楼高数据
                    Map<String, DynamicFieldsEntity> maxMap = maxTitle.stream().collect(
                            Collectors.toMap(DynamicFieldsEntity::getCbrsId,
                                    w -> w));
                    buildingHeightDtos = allSrBaseline.stream().map(h -> {
                        return toBuildingHeight(maxMap.get(h.getProjectId()), h);
                    }).collect(Collectors.toList());

                    for (BuildingHeightDto dto : buildingHeightDtos) {
                        int b = 3;
                        if (intensity == 10) {
                            b = 2;
                        }
                        if (getMultiple(it.getInFloor()) == (dto.getLength()) && mainTableUtils.getValueByKey("smxcdlb", it.getSite()).equals(dto.getType())) {
                            valueEntity.setAo(dto.getValue());
                            maxIds.append(valueEntity.getId());
                        }
                        //k1取值
                        BigDecimal divide = new BigDecimal(b).multiply(it.getAboveGroundHeight()).divide(BigDecimal.valueOf(it.getInFloor()), 4, RoundingMode.HALF_UP);
                        valueEntity.setK1(BigDecimal.valueOf(1).add(divide));
                    }

                    if (PlatformObjectUtils.isEmpty(valueEntity.getAo())) {
                        return RestResponse.fail("失败!请在 【通讯设备系数维护】 模块中维护 {地震影晌系数基准值} 信息");
                    }
                    //K2取值
                    valueEntity.setK2(BigDecimal.valueOf(getLinearInterpolationValue(it.getNaturalPeriod().doubleValue())).setScale(4, RoundingMode.HALF_UP));
                    //amax值
                    valueEntity.setAmax(BigDecimal.valueOf(0.5));
                    if (intensity == 6) {
                        valueEntity.setAmax(BigDecimal.valueOf(0.063));
                    }
                    if (intensity == 7) {
                        valueEntity.setAmax(BigDecimal.valueOf(0.125));
                    }
                    if (intensity == 8) {
                        valueEntity.setAmax(BigDecimal.valueOf(0.25));
                    }
                    //a值
                    valueEntity.setResult(valueEntity.getAo().multiply(valueEntity.getAmax()));
                    //FH值
                    BigDecimal fh = valueEntity.getK1().multiply(valueEntity.getK2()).multiply(valueEntity.getK3()).multiply(valueEntity.getResult()).multiply(it.getWeight()).setScale(4, RoundingMode.HALF_UP);
                    valueEntity.setFh(fh);
                    if (PlatformObjectUtils.isEmpty(valueEntity.getFhCorrect())) {
                        valueEntity.setFhCorrect(fh);
                    }

                    if (intensity > 8) {
                        BigDecimal fv = BigDecimal.valueOf(0.65).multiply(valueEntity.getK1()).multiply(valueEntity.getK2()).multiply(valueEntity.getK3()).multiply(valueEntity.getResult()).multiply(it.getWeight()).setScale(4, RoundingMode.HALF_UP);
                        valueEntity.setFv(fv);
                        if (PlatformObjectUtils.isEmpty(valueEntity.getFvCorrect())) {
                            valueEntity.setFvCorrect(fv);
                        }
                    }
                    ceVulnerabilityRepository.saveDevicesValue(valueEntity);
                }
                DestructiveStateVo stateVo = ceVulnerabilityRepository.getByBasicInfo(it);
                mainTableUtils.updateCalculationStatus(it.getTableId(),stateVo.getDisasterRate6State(),stateVo.getDisasterRate7State(),stateVo.getDisasterRate8State(),stateVo.getDisasterRate9State(),stateVo.getDisasterRate10State(), 1);
                mainTableUtils.updateModelBaseIds(k3Ids.toString(), maxIds.toString(), it.getTableId());
            }
            mainTableUtils.updateVersionCalculationStatus(versionId, 0);
            return RestResponse.succeed("计算成功!");
        } catch (Exception e) {
            log.error("get caCeVulnerability error:{},versionId:{}", e.getMessage(), versionId, e);
            return RestResponse.fail("数据异常,计算失败!");
        }
    }

    /**
     * 线性插值
     *
     * @return
     */
    private static double getLinearInterpolationValue(double T) {
        if (T <= 0.01) {
            return 1;
        } else if (T == 1) {
            return 2;
        } else if (T >= 0.2 && T <= 0.7) {
            return 3;
        } else if (T > 0.01 && T < 0.2) {
            double k1 = 1;
            double k2 = 3;
            double t1 = 0.01;
            double t2 = 0.2;
            return linearInterpolation(k1, k2, t1, t2, T);
        } else if (T > 0.7 && T < 1) {
            double k1 = 3;
            double k2 = 2;
            double t1 = 0.7;
            double t2 = 1;
            return linearInterpolation(k1, k2, t1, t2, T);
        }
        throw new IllegalArgumentException("无效的T值");
    }

    private static double linearInterpolation(double k1, double k2, double t1, double t2, double T) {
        return k1 + (k2 - k1) * (T - t1) / (t2 - t1);
    }


    /**
     * 取5的倍数
     *
     * @param a
     * @return
     */
    public static int getMultiple(int a) {
        return (a % 5 == 0) ? a : ((a / 5 + 1) * 5);
    }


    private BuildingHeightDto toBuildingHeight(DynamicFieldsEntity dynamicFieldsEntity, SrBaselineEntity h) {
        BuildingHeightDto buildingHeightDto = new BuildingHeightDto();
        buildingHeightDto.setId(h.getCsrbId());
        buildingHeightDto.setProjectId(dynamicFieldsEntity.getCbrsId());
        buildingHeightDto.setType(dynamicFieldsEntity.getInfo());
        buildingHeightDto.setValue(h.getSiteValue());
        buildingHeightDto.setLength(Integer.valueOf(h.getSite()));
        return buildingHeightDto;
    }

    private CaCeVulnerabilityDto toCaCeVulnerability(DynamicFieldsEntity dynamicFieldsEntity, CnRatioEntity cnRatioEntity) {
        CaCeVulnerabilityDto dto = new CaCeVulnerabilityDto();
        dto.setId(cnRatioEntity.getCcrId());
        dto.setProjectId(dynamicFieldsEntity.getCbrsId());
        dto.setRegion(dynamicFieldsEntity.getInfo());
        dto.setValue(cnRatioEntity.getCategoryValue());
        dto.setType(cnRatioEntity.getCategory());
        return dto;
    }

    @Override
    public RestResponse importCeVulnerability(MultipartFile file) {
        try {
            ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
            List<List<Object>> read = reader.read(1, reader.getRowCount());
            List<Object> title = reader.readRow(0);
            String titleName = "[层数, 地震烈度, 楼层的地上高度, W为设备自身及加固件总重量（N）, T设备自振周期]";
            if (!titleName.equals(title.toString())) {
                return RestResponse.fail("导入失败，请检查表头是否改动");
            }
            if (read.size() == 0) {
                return RestResponse.fail("导入失败，表格中无数据");
            }
            //数据校验
            RestResponse rs = CheckUtil.check(1, read.size(), read, titleName);
            if (rs.getCode() != 200) {
                return RestResponse.fail(rs.getMessage());
            }
            ArrayList<CsForceImportVo> dataList = new ArrayList<>();
            read.forEach(it -> {
                CsForceImportVo entity = new CsForceImportVo();
                entity.setStorey(Integer.valueOf(it.get(0).toString()));
                entity.setEarthquakeIntensity(Integer.valueOf(it.get(1).toString()));
                entity.setHeight(new BigDecimal(it.get(2).toString()));
                entity.setTwEquipment(new BigDecimal(it.get(3).toString()));
                entity.setT(new BigDecimal(it.get(4).toString()));
                dataList.add(entity);
            });
            return RestResponse.succeed(dataList);
        } catch (IOException e) {
            String errorMessage = "数据异常,导入失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> uploadDevices(String fileIds, String versionId, String userId) throws Exception {
        Map<String, Object> map = new HashMap<>();
        shpUtils.uniformFileName(fileIds);
        ParsingResult shpData = shpUtils.getShpData(fileIds);
        //是否成功
        if (!shpData.getMessage().equals("ok")) {
            map.put("res", false);
            map.put("mess", shpData.getMessage());
        }
        List<List<Object>> shpList = shpData.getShpList();
        List<SmxDevicesEntity> devicesList = new ArrayList<>();
        for (int i = 0; i < shpList.size(); i++) {
            String id = UUIDGenerator.getUUID();
            List<Object> list = shpList.get(i);
            if (list.size() < 21) {
                return null;
            }
            SmxDevicesEntity entity = new SmxDevicesEntity();
            entity.setDelFlag(YNEnum.N.toString());
            entity.setId(UUIDGenerator.getUUID());
            entity.setTableId(id);
            entity.setCreateUser(userId);
            entity.setCreateTime(PlatformDateUtils.getCurrentTimestamp());

            if (PlatformObjectUtils.isNotEmpty(list.get(0))) {
                entity.setGeom(list.get(0).toString());
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(1))) {
                entity.setInObjectName(ShpAnalyticUtil.getStringVal(list.get(2).toString(), "所属建筑物名称", true));
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(2))) {
                entity.setLocation(ShpAnalyticUtil.getStringVal(list.get(2).toString(), "所属建筑物地址位置", true));
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(3))) {
                entity.setObjectLon(ShpAnalyticUtil.getStringVal(list.get(3).toString(), "所属建筑物地理经度坐标", true));
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(4))) {
                entity.setObjectLat(ShpAnalyticUtil.getStringVal(list.get(4).toString(), "所属建筑物地理纬度坐标", true));
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(5))) {
                entity.setDeviceType(ShpAnalyticUtil.getStringVal(list.get(5).toString(), "设备类别", true));
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(6))) {
                entity.setDeviceName(ShpAnalyticUtil.getStringVal(list.get(6).toString(), "设备名称", true));
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(7))) {
                entity.setModel(ShpAnalyticUtil.getStringVal(list.get(7).toString(), "型号", true));
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(8))) {
                entity.setInFloor(ShpAnalyticUtil.getIntegerVal(list.get(8).toString(), "所在楼层", true));
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(9))) {
                entity.setInstallationSituation(ShpAnalyticUtil.getStringVal(list.get(9).toString(), "安装情况", true));
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(10))) {
                entity.setFixedForm(ShpAnalyticUtil.getStringVal(list.get(10).toString(), "设备固定形式", true));
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(11))) {
                entity.setBuiltEra(ShpAnalyticUtil.getIntegerVal(list.get(11).toString(), "建设年代", true));
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(12))) {
                entity.setFortification(list.get(12).toString());
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(13))) {
                entity.setBoltDiameter(new BigDecimal(list.get(13).toString()));
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(14))) {
                entity.setPurchaseCost(new BigDecimal(list.get(14).toString()));
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(15))) {
                entity.setManufacturers(list.get(15).toString());
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(16))) {
                entity.setSize(list.get(16).toString());
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(17))) {
                entity.setWeight(new BigDecimal(list.get(17).toString()));
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(18))) {
                entity.setAboveGroundHeight(new BigDecimal(list.get(18).toString()));
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(19))) {
                entity.setNaturalPeriod(new BigDecimal(list.get(19).toString()));
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(20))) {
                entity.setSite(list.get(20).toString());
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(21))) {
                entity.setRegion(list.get(21).toString());
            }
            if (PlatformObjectUtils.isNotEmpty(list.get(22))) {
                entity.setTotalHeight(new BigDecimal(list.get(22).toString()));
            }
            if (list.size() > 23 && !PlatformObjectUtils.isEmpty(list.get(23))) {
                entity.setExtends1(list.get(23).toString());
            }
            if (list.size() > 24 && !PlatformObjectUtils.isEmpty(list.get(24))) {
                entity.setExtends2(list.get(24).toString());
            }
            if (list.size() > 25 && !PlatformObjectUtils.isEmpty(list.get(25))) {
                entity.setExtends3(list.get(25).toString());
            }
            if (list.size() > 26 && !PlatformObjectUtils.isEmpty(list.get(26))) {
                entity.setExtends4(list.get(26).toString());
            }
            if (list.size() > 27 && !PlatformObjectUtils.isEmpty(list.get(27))) {
                entity.setExtends5(list.get(27).toString());
            }
            if (list.size() > 28 && !PlatformObjectUtils.isEmpty(list.get(28))) {
                entity.setExtends6(list.get(28).toString());
            }
            if (list.size() > 29 && !PlatformObjectUtils.isEmpty(list.get(29))) {
                entity.setExtends7(list.get(29).toString());
            }
            if (list.size() > 30 && !PlatformObjectUtils.isEmpty(list.get(30))) {
                entity.setExtends8(list.get(30).toString());
            }
            if (list.size() > 31 && !PlatformObjectUtils.isEmpty(list.get(31))) {
                entity.setExtends9(list.get(31).toString());
            }
            if (list.size() > 32 && !PlatformObjectUtils.isEmpty(list.get(32))) {
                entity.setExtends10(list.get(32).toString());
            }
            entity.setVersionId(versionId);
            devicesList.add(entity);

            SmxMainTableEntity mainEntity = new SmxMainTableEntity();
            mainEntity.setId(id);
            mainEntity.setYear(entity.getBuiltEra());
            mainEntity.setCreateUser(userId);
            mainEntity.setVersionId(versionId);
            mainEntity.setFortification(entity.getFortification());
            mainEntity.setBusinessObject(FxfzConstants.CZT_SMX_TX);
            mainTableUtils.saveMainTable(mainEntity);
        }
        boolean res = false;

        int i = 1;
        for (SmxDevicesEntity entity : devicesList) {
            entity.setSerialNumber(i);
            res = ceVulnerabilityRepository.saveDevices(entity);
            i += 1;
        }
        map.put("res", res);
        return map;
    }

    @Override
    public HashMap<Integer, ArrayList<String>> getDataValidation(String versionId) {
        HashMap<Integer, ArrayList<String>> map = new HashMap<>();
        SmxVersionEntity versionEntity = lifelineEngineeringRepository.getById(versionId);
        if (PlatformObjectUtils.isNotEmpty(versionEntity)) {
            List<SmxDevicesEntity> roadList = ceVulnerabilityRepository.getByVersionId(versionEntity.getId());
            for (SmxDevicesEntity entity : roadList) {
                ArrayList<String> list = ValidationProcessor.validate(entity);
                if (list.size() != 0) {
                    map.put(entity.getSerialNumber(), list);
                }
            }
        }
        return map;
    }

    @Override
    public SmxDevicesEntity getByTableId(String id) {
        return ceVulnerabilityRepository.getByTableId(id);
    }

    @Override
    public List<CztSmxDevicesValueEntity> getValueByDevicesId(String id) {
        return ceVulnerabilityRepository.getValueByDevicesId(id);
    }

    @Override
    public void updateDevicesById(DevicesParam devicesParam) {
        ceVulnerabilityRepository.updateDevicesById(devicesParam);
    }

    @Override
    public RestResponse count(DevicesParam param) {
        Integer[] intensitys = {6, 7, 8, 9, 10};
        CommunicationCountVo vo = new CommunicationCountVo();
        //k3
        List<DynamicFieldsEntity> k3Title = zqtCiRepository.getDynamicFields("通讯设备重要度系数");
        List<CnRatioEntity> allCnRatio = cecMainRepository.getAllCnRatio();
        //amx
        List<DynamicFieldsEntity> maxTitle = zqtCiRepository.getDynamicFields("地震影响系数基准值");
        List<SrBaselineEntity> allSrBaseline = cecMainRepository.getAllSrBaseline();
        ArrayList<CztSmxDevicesValueEntity> list = new ArrayList<>();

        StringBuilder k3Ids = new StringBuilder();
        StringBuilder maxIds = new StringBuilder();
        for (Integer intensity : intensitys) {
            CztSmxDevicesValueEntity valueEntity = new CztSmxDevicesValueEntity();
            valueEntity.setIntensity(intensity);
            List<CaCeVulnerabilityDto> caCeVulnerabilityDtos;
            List<BuildingHeightDto> buildingHeightDtos;
            //处理k3数据
            Map<String, DynamicFieldsEntity> map = k3Title.stream().collect(
                    Collectors.toMap(DynamicFieldsEntity::getCbrsId,
                            w -> w));
            caCeVulnerabilityDtos = allCnRatio.stream().map(h -> {
                return toCaCeVulnerability(map.get(h.getProjectId()), h);
            }).collect(Collectors.toList());

            for (CaCeVulnerabilityDto dto : caCeVulnerabilityDtos) {
                if (mainTableUtils.getValueByKey("smxtxsblb", param.getDeviceType()).equals(dto.getType()) && mainTableUtils.getValueByKey("smxtxsbszqy", param.getRegion()).equals(dto.getRegion())) {
                    String value = dto.getValue();
                    k3Ids.append(dto.getId());
                    if (value.contains("~")) {
                        BigDecimal bigDecimal = new BigDecimal(value.substring(0, value.indexOf("~"))).add(BigDecimal.valueOf(0.05));
                        valueEntity.setK3(bigDecimal);
                    } else {
                        valueEntity.setK3(new BigDecimal(value));
                    }
                }
            }
            if (PlatformObjectUtils.isEmpty(valueEntity.getK3())) {
                return RestResponse.fail("失败!请在 【通讯设备系数维护】 模块中维护 {通讯设备重要度系数} 信息");
            }

            //处理ao和楼高数据
            Map<String, DynamicFieldsEntity> maxMap = maxTitle.stream().collect(
                    Collectors.toMap(DynamicFieldsEntity::getCbrsId,
                            w -> w));
            buildingHeightDtos = allSrBaseline.stream().map(h -> {
                return toBuildingHeight(maxMap.get(h.getProjectId()), h);
            }).collect(Collectors.toList());

            for (BuildingHeightDto dto : buildingHeightDtos) {
                int b = 3;
                if (intensity == 10) {
                    b = 2;
                }
                if (getMultiple(param.getInFloor()) == (dto.getLength()) && mainTableUtils.getValueByKey("smxcdlb", param.getSite()).equals(dto.getType())) {
                    valueEntity.setAo(dto.getValue());
                    maxIds.append(valueEntity.getId());
                }
                //k1取值
                BigDecimal divide = new BigDecimal(b).multiply(param.getAboveGroundHeight()).divide(BigDecimal.valueOf(param.getInFloor()), 4, RoundingMode.HALF_UP);
                valueEntity.setK1(BigDecimal.valueOf(1).add(divide));
            }

            if (PlatformObjectUtils.isEmpty(valueEntity.getAo())) {
                return RestResponse.fail("失败!请在 【通讯设备系数维护】 模块中维护 {地震影晌系数基准值} 信息");
            }
            //K2取值
            valueEntity.setK2(BigDecimal.valueOf(getLinearInterpolationValue(param.getNaturalPeriod().doubleValue())).setScale(4, RoundingMode.HALF_UP));
            //amax值
            valueEntity.setAmax(BigDecimal.valueOf(0.5));
            if (intensity == 6) {
                valueEntity.setAmax(BigDecimal.valueOf(0.063));
            }
            if (intensity == 7) {
                valueEntity.setAmax(BigDecimal.valueOf(0.125));
            }
            if (intensity == 8) {
                valueEntity.setAmax(BigDecimal.valueOf(0.25));
            }
            //a值
            valueEntity.setResult(valueEntity.getAo().multiply(valueEntity.getAmax()));
            //FH值
            BigDecimal fh = valueEntity.getK1().multiply(valueEntity.getK2()).multiply(valueEntity.getK3()).multiply(valueEntity.getResult()).multiply(param.getWeight()).setScale(4, RoundingMode.HALF_UP);
            valueEntity.setFh(fh);
            if (PlatformObjectUtils.isEmpty(valueEntity.getFhCorrect())) {
                valueEntity.setFhCorrect(fh);
            }

            if (intensity > 8) {
                BigDecimal fv = BigDecimal.valueOf(0.65).multiply(valueEntity.getK1()).multiply(valueEntity.getK2()).multiply(valueEntity.getK3()).multiply(valueEntity.getResult()).multiply(param.getWeight()).setScale(4, RoundingMode.HALF_UP);
                valueEntity.setFv(fv);
                if (PlatformObjectUtils.isEmpty(valueEntity.getFvCorrect())) {
                    valueEntity.setFvCorrect(fv);
                }
            }
            list.add(valueEntity);
        }
        param.setK3Ids(k3Ids.toString());
        param.setMaxIds(maxIds.toString());
        vo.setParam(param);
        vo.setList(list);
        return RestResponse.succeed(vo);
    }
}
